<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>命令ID管理 - 管理界面</title>
    <style>
    .container {
        max-width: 1200px;
        margin: 0 auto;
        padding: 2rem;
    }

    .section-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 2rem;
    }

    .table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 1rem;
    }

    .table th,
    .table td {
        padding: 0.75rem;
        border: 1px solid #dee2e6;
    }

    .table th {
        background-color: #f8f9fa;
        font-weight: 600;
    }

    .actions {
        display: flex;
        gap: 0.5rem;
    }

    .btn {
        display: inline-block;
        padding: 0.5rem 1rem;
        border-radius: 4px;
        text-decoration: none;
        cursor: pointer;
        border: none;
        font-size: 1rem;
    }

    .btn-primary {
        background-color: #007bff;
        color: white;
    }

    .btn-danger {
        background-color: #dc3545;
        color: white;
    }

    .btn-sm {
        padding: 0.25rem 0.5rem;
        font-size: 0.875rem;
    }

    .alert {
        padding: 1rem;
        margin-bottom: 1rem;
        border-radius: 4px;
    }

    .alert-success {
        background-color: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
    }

    .alert-danger {
        background-color: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }

    .alert-info {
        background-color: #d1ecf1;
        color: #0c5460;
        border: 1px solid #bee5eb;
    }
    </style>
</head>
<body>
    <div class="container">
        <div class="section-header">
            <h1>子模块列表</h1>
            <a href="/db/commands/add" class="btn btn-primary">添加命令</a>
        </div>
        <div id="cmdList"></div>
    </div>

    <script>
        // 获取子模块列表
        async function fetchCmdList() {
            try {
                const response = await fetch('/api/commands');
                const data = await response.json();
                if (data.status === 'success') {
                    displayCmdList(data.data);
                } else {
                    showError('获取子模块列表失败');
                }
            } catch (error) {
                console.error('获取子模块列表失败:', error);
                showError('获取子模块列表失败');
            }
        }

        // 显示子模块列表
        function displayCmdList(cmds) {
            const cmdList = document.getElementById('cmdList');
            if (cmds.length === 0) {
                cmdList.innerHTML = '<div class="alert alert-info">当前无子模块ID说明信息</div>';
                return;
            }

            const table = document.createElement('table');
            table.className = 'table';
            
            // 表头
            const thead = document.createElement('thead');
            thead.innerHTML = `
                <tr>
                    <th>子模块ID(cmdid)</th>
                    <th>命令说明</th>
                    <th>操作</th>
                </tr>
            `;
            table.appendChild(thead);
            
            // 表格内容
            const tbody = document.createElement('tbody');
            cmds.forEach(cmd => {
                const tr = document.createElement('tr');
                tr.innerHTML = `
                    <td>${cmd.cmdid}</td>
                    <td>${cmd.content}</td>
                    <td class="actions">
                        <a href="/db/commands/edit?id=${cmd.id}" class="btn btn-sm btn-primary">编辑</a>
                        <button onclick="deleteCmd(${cmd.id})" class="btn btn-sm btn-danger">删除</button>
                    </td>
                `;
                tbody.appendChild(tr);
            });
            table.appendChild(tbody);
            
            cmdList.innerHTML = '';
            cmdList.appendChild(table);
        }

        // 删除命令
        async function deleteCmd(id) {
            if (confirm('确定要删除这个命令吗？')) {
                try {
                    const response = await fetch(`/api/commands/${id}`, {
                        method: 'DELETE'
                    });
                    const data = await response.json();
                    if (data.status === 'success') {
                        showSuccess('删除成功');
                        fetchCmdList(); // 刷新列表
                    } else {
                        showError('删除失败: ' + data.message);
                    }
                } catch (error) {
                    console.error('删除命令失败:', error);
                    showError('删除失败');
                }
            }
        }

        // 显示成功消息
        function showSuccess(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-success';
            alert.textContent = message;
            document.querySelector('.container').insertBefore(alert, document.getElementById('cmdList'));
            setTimeout(() => alert.remove(), 3000);
        }

        // 显示错误消息
        function showError(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-danger';
            alert.textContent = message;
            document.querySelector('.container').insertBefore(alert, document.getElementById('cmdList'));
            setTimeout(() => alert.remove(), 3000);
        }

        // 页面加载时获取子模块列表
        document.addEventListener('DOMContentLoaded', fetchCmdList);
    </script>
</body>
</html>

